package com.wen.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.wen.servicebase.exception.BusinessException;
import com.wen.vod.service.VodService;
import com.wen.vod.util.InitVodClient;
import com.wen.vod.util.VodProperties;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author：Win
 * @Date: 2021/6/11 15:00
 * @Description:
 */
@Service
public class VodServiceImpl implements VodService {
    /**
     * 上传视频到阿里云的方法
     *
     * @param file 需要上传的视频
     * @return 上传成功后的视频id
     */
    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            String filename = file.getOriginalFilename();
            assert filename != null;
            String title = filename.substring(0, filename.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET, title, filename, file.getInputStream());
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            return response.getVideoId();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除单个视频
     *
     * @param id 视频id
     */
    @Override
    public void deleteSingleAlyVideo(String id) {
        try {
//        初始化client对象
            DefaultAcsClient client = InitVodClient.initVodClient(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET);
//        创建删除阿里云视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
//        向request设置要删除的视频id,可以设置多个,用逗号隔开
            request.setVideoIds(id);
//        调用初始化client对象删除视频
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new BusinessException("删除视频失败~~~");
        }
    }

    /**
     * 删除多个视频
     *
     * @param ids 视频id集合
     */
    @Override
    public void deleteMultiAlyVideo(List<String> ids) {
        try {
//        初始化client对象
            DefaultAcsClient client = InitVodClient.initVodClient(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET);
//        创建删除阿里云视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
//        向request设置要删除的视频id,可以设置多个,用逗号隔开
            String list = StringUtils.join(ids.toArray(), ",");
            request.setVideoIds(list);
//        调用初始化client对象删除视频
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new BusinessException("删除视频失败~~~");
        }
    }
}
